package cn.ylyhappy.ideavimbuilder4;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class TextSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable {

    private SurfaceHolder mHolder;
    private Thread mDrawThread;
    private boolean mIsRunning;
    private Paint mTextPaint;
    private String mDisplayText = "Hello SurfaceView!";
    private int mTextX = 100;
    private int mTextY = 100;
    private int mTextDirection = 1;

    public TextSurfaceView(Context context) {
        this(context, null);
    }

    public TextSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mHolder = getHolder();
        mHolder.addCallback(this);

        mTextPaint = new Paint();
        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setTextSize(60);
        mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
        mTextPaint.setAntiAlias(true);
    }

    public void setDisplayText(String text) {
        mDisplayText = text;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mIsRunning = true;
        mDrawThread = new Thread(this);
        mDrawThread.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        mTextY = height / 2; // 初始垂直位置居中
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mIsRunning = false;
        try {
            mDrawThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (mIsRunning) {
            draw();
            updateTextPosition();
            try {
                Thread.sleep(16); // 约60帧/秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void draw() {
        Canvas canvas = mHolder.lockCanvas();
        if (canvas == null) return;

        try {
            // 清空画布
            canvas.drawColor(Color.BLACK);
            
            // 绘制文字
            canvas.drawText(mDisplayText, mTextX, mTextY, mTextPaint);
            
            // 绘制辅助信息
            Paint infoPaint = new Paint();
            infoPaint.setColor(Color.GRAY);
            infoPaint.setTextSize(30);
            canvas.drawText("FPS: 60", 20, 50, infoPaint);
            canvas.drawText("Text Position: " + mTextX + ", " + mTextY, 20, 90, infoPaint);
        } finally {
            mHolder.unlockCanvasAndPost(canvas);
        }
    }

    private void updateTextPosition() {
        // 水平移动
        mTextX += 5 * mTextDirection;
        
        // 边界检测
        if (mTextX > getWidth() - mTextPaint.measureText(mDisplayText)) {
            mTextDirection = -1;
        } else if (mTextX < 0) {
            mTextDirection = 1;
        }
    }
}